package controller

import (
	"RocFamilySystem/internal/service"
	"RocFamilySystem/models"
	"RocFamilySystem/pkg/constant"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

type SysPermissionController struct {
	BaseController
	SysPermissionService *service.SysPermissionService
}

// 初始化
func init() {
	registerContainer["SysPermissionController"] = &SysPermissionController{}
}

// SetService 设置服务
func (controller *SysPermissionController) SetService(sysSysPermissionService *service.SysPermissionService) {
	controller.SysPermissionService = sysSysPermissionService
}

// RegisterRouter 注册路由
func (controller *SysPermissionController) RegisterRouter(engine *gin.Engine) {
	prefix := "/sysPermission"
	g := engine.Group(prefix)
	prefixPermission := "permission" + strings.ReplaceAll(prefix, "/", ":")
	{
		//新增系统权限
		g.POST("", controller.AddSysPermission)
		registerPermissionContainer("新增系统权限", prefixPermission+":create", prefix, "POST")
		//删除系统权限
		g.DELETE("/:id", controller.RemoveSysPermission)
		registerPermissionContainer("ID删除系统权限", prefixPermission+":delete", prefix, "DELETE")
		g.DELETE("/batchDelete", controller.RemoveSysPermissionBatch)
		registerPermissionContainer("删除系统权限", prefixPermission+":batchDelete", prefix, "DELETE")
		//修改系统权限
		g.PUT("", controller.ModifySysPermission)
		registerPermissionContainer("修改系统权限", prefixPermission+":update", prefix, "PUT")
		//获取系统权限列表
		g.POST("/list", controller.QuerySysPermission)
		registerPermissionContainer("查询系统权限列表", prefixPermission+":list", prefix, "POST")
		//通过ID查询系统权限
		g.GET("/:id", controller.QuerySysPermissionById)
		registerPermissionContainer("ID查询系统权限", prefixPermission+":id", prefix, "GET")
	}
}

// AddSysPermission 添加系统权限
// @Summary AddSysPermission 添加系统权限
// @Tags 系统权限
// @Produce json
// @Param sysSysPermission body models.SysPermission true "系统权限"
// @Success 200 {object} Result "成功"
// @Router /sysPermission [post]
func (controller *SysPermissionController) AddSysPermission(ctx *gin.Context) {
	var sysSysPermission models.SysPermission
	err := ctx.ShouldBind(&sysSysPermission)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.SysPermissionService.AddSysPermission(&sysSysPermission)
	controller.CustomResult(ctx, 200, "添加系统权限成功", nil)
}

// RemoveSysPermission 删除系统权限
// @Summary RemoveSysPermission  删除系统权限
// @Tags 系统权限
// @Produce json
// @Param id path int true "系统权限id"
// @Success 200 {object} Result "成功"
// @Router /sysPermission/:id [delete]
func (controller *SysPermissionController) RemoveSysPermission(ctx *gin.Context) {
	sysSysPermissionId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.SysPermissionService.RemoveSysPermission(sysSysPermissionId)
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%d】成功", sysSysPermissionId), nil)
}

// RemoveSysPermissionBatch 批量删除系统权限
// @Summary RemoveSysPermission  批量删除系统权限
// @Tags 系统缓存
// @Produce json
// @Param ids[] query []int true "系统缓存id"
// @Success 200 {object} Result "成功"
// @Router /sysPermission/batchDelete [delete]
func (controller *SysPermissionController) RemoveSysPermissionBatch(ctx *gin.Context) {
	ids := ctx.QueryArray("ids[]")
	if len(ids) == 0 {
		controller.HandleError(errors.New("参数缺失"), constant.PARAMS_ERROR)
	}
	controller.SysPermissionService.RemoveSysPermissionBatch(ids)
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%v】成功", ids), nil)
}

// ModifySysPermission 修改系统权限
// @Summary ModifySysPermission 修改系统权限
// @Tags 系统权限
// @Produce json
// @Param sysSysPermission body models.SysPermission true "系统权限"
// @Success 200 {object} Result "成功"
// @Router /sysPermission [put]
func (controller *SysPermissionController) ModifySysPermission(ctx *gin.Context) {
	var sysSysPermission models.SysPermission
	err := ctx.ShouldBind(&sysSysPermission)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.SysPermissionService.ModifySysPermission(&sysSysPermission)
	controller.CustomResult(ctx, 200, fmt.Sprintf("修改【%d】成功", *sysSysPermission.Id), nil)
}

// QuerySysPermission 查询系统权限列表
// @Summary QuerySysPermission 查询系统权限列表
// @Tags 系统权限
// @Produce json
// @Param sysSysPermission body models.SysPermission true "系统权限"
// @Success 200 {object} Result "成功"
// @Router /sysPermission/list [post]
func (controller *SysPermissionController) QuerySysPermission(ctx *gin.Context) {
	pageNum, pageSize := controller.GetPageInfo(ctx)
	var sysSysPermission models.SysPermission
	err := ctx.ShouldBind(&sysSysPermission)
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.SysPermissionService.QuerySysPermission(&sysSysPermission, pageNum, pageSize)
	controller.SuccessResult(ctx, data)
}

// QuerySysPermissionById 查询系统权限通过ID
// @Summary QuerySysPermissionById  查询系统权限通过ID
// @Tags 系统权限
// @Produce json
// @Param id path int true "系统权限id"
// @Success 200 {object} Result "成功"
// @Router /sysPermission/:id [get]
func (controller *SysPermissionController) QuerySysPermissionById(ctx *gin.Context) {
	sysSysPermissionId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.SysPermissionService.QuerySysPermissionById(sysSysPermissionId)
	controller.SuccessResult(ctx, data)
}
